package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import networld.NetWorld;
	import networld.NetWorldEvent;
	
	/**
	 * ...
	 * @author 
	 */
	public class Main extends Sprite 
	{
		private const developerKey: String = "27c84d59efabfd20ac6d1b29-597cce2639ff"
		private const coordinationServerAddress: String = "pilebox.ru";
		private const coordinationServerPort: Number = 11717;
		
		private var sendButton: TextField;
		private var chatViewer: TextField;
		private var chatEditor: TextField;
		
		private var world: NetWorld;
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			var log: TextField = new TextField();
			log.width = stage.stageWidth;
			log.height = stage.stageHeight;
			addChild(log);
			
			createGUI();
			/* It is only pseudo-code for designing library */
			world = new NetWorld(log, coordinationServerAddress,
												coordinationServerPort, developerKey);
			
			world.addEventListener(NetWorldEvent.CHAT_MESSAGE, displayMessage);
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
		}
		
		private function displayMessage(e: NetWorldEvent): void {
			chatViewer.appendText(e.messageText);
		}
		private function createGUI(): void {
			sendButton = new TextField();
			sendButton.selectable = false;
			sendButton.x = 100;
			sendButton.y = 100;
			sendButton.width = 100;
			sendButton.height = 25;
			sendButton.text = "Send";
			
			sendButton.addEventListener(MouseEvent.CLICK,
				function (e: MouseEvent):void 
				{
					world.say(chatEditor.text);
				});
			
			chatViewer = new TextField();
			chatViewer.x = sendButton.x + sendButton.width;
			chatViewer.y = sendButton.y + sendButton.height;
			chatViewer.width = 600; 	chatViewer.height = 200;
			chatViewer.wordWrap = true;
			
			chatEditor = new TextField();
			chatEditor.x = sendButton.x + sendButton.width;
			chatEditor.y = chatViewer.y + chatViewer.height;
			chatEditor.width = 600; chatEditor.height = 200;
			chatEditor.border = true; chatEditor.thickness = 1;	
			chatEditor.type = TextFieldType.INPUT;
			chatEditor.wordWrap = true;
			
			addChild(sendButton);
			addChild(chatViewer);
			addChild(chatEditor);
		}
		
	}
	
}